//
// Created by RandBII on 2020/12/30.
//

#ifndef FFMPEGFORANDROID_VIDEOGLRENDER_H
#define FFMPEGFORANDROID_VIDEOGLRENDER_H


#include <__mutex_base>
#include "../BaseGLRender.h"
#include "VideoRender.h"
#include "../../../utils/ImageDefine.h"
#include <thread>
#include <GLES3/gl3.h>
#include <detail/type_mat.hpp>
#include <detail/type_mat4x4.hpp>
#include <vec2.hpp>
#include "GLHelper.h"
#include <gtc/matrix_transform.inl>

#define TEXTURE_NUM 3
#define MATH_PI 3.1415926535897932384626433832802

using namespace std;
using namespace glm;

class VideoGLRender : public VideoRender, public BaseGLRender {

public:
    virtual void Init(int videoWidth, int height, int *dstSize) override;

    virtual void RenderVideoFrame(NativeImage *pImg) override;

    virtual void UnInit() override;

    virtual void OnSurfaceCreated() override;

    virtual void OnSurfaceChanged(int width, int height) override;

    virtual void OnDrawFrame() override;

    static VideoGLRender *GetInstance();

    static void ReleaseInstance();

    virtual void UpdateMVPMatrix(int angelX, int angelY, int scaleX, int scaleY) override;

    virtual void SetTouchLoc(float touchX, float touchY) override;

private:
    VideoGLRender();

    ~VideoGLRender();

    static mutex mMutex;

    static VideoGLRender *mInstance;
    GLuint mProgramObj = GL_NONE;
    GLuint mTextureIds[TEXTURE_NUM];
    GLuint mVaoId;
    GLuint mVboIds[3];
    NativeImage mRenderImage;
    glm::mat4 mMVPMatrix;

    int mFrameIndex;
    vec2 mTouchXY;
    vec2 mScreenSize;

};


#endif //FFMPEGFORANDROID_VIDEOGLRENDER_H
